import { request } from '@/service/request';
import type { Backend } from '@/service/backend';

// 定义请求数据的接口
interface SignRequestData {
  username: string;
  password: string;
  age: string;
  sex: string;
  phone: string;
  avatar: string;
  company: string;
}

interface LoginRequestData {
  username: string;
  password: string;
}

interface AcheckRequestData{
  camera_id: string, 
image: string,
type: number, 
confidence: number, 
code: string, 
result: boolean, 
is_artificial: boolean
}

export const StatusApi = {
  login: "/user/login/",
  sign: "/user/register/",
  acheck:"/detection/post_data/"
};

// 注册
export const Sign = async (requestData: SignRequestData) => {
  const requestBody: SignRequestData = {
    username: requestData.username,
    password: requestData.password,
    age: requestData.age,
    sex: requestData.sex,
    phone: requestData.phone,
    avatar: requestData.avatar,
    company: requestData.company,
  };

  try {
    const res = await request.post<Backend.sign>(StatusApi.sign, requestBody);  // 响应类型为 Backend.sign
    return res;
  } catch (error) {
    console.error("Sign 请求失败:", error);
    return null; 
  }
};

// 登录
export const Login = async (requestData: LoginRequestData) => {
  const requestBody: LoginRequestData = {
    username: requestData.username,
    password: requestData.password,
  };

  try {
    const res = await request.post<Backend.login>(StatusApi.login, requestBody);  // 响应类型为 Backend.login
    return res;
  } catch (error) {
    console.error("Login 请求失败:", error);
    return null; 
  }
};

//人工检测
export const Acheck = async (requestData: AcheckRequestData) => {
  const requestBody: AcheckRequestData = {
    camera_id: requestData.camera_id, 
    image: requestData.image,
    type: requestData.type,
    confidence: requestData.confidence, 
    code: requestData.code,
    result: requestData.result, 
    is_artificial: requestData.is_artificial,
  };

  try {
    const res = await request.post<Backend.acheck>(StatusApi.acheck, requestBody);  
    return res;
  } catch (error) {
    console.error("Login 请求失败:", error);
    return null; 
  }
};